package com.glsc.ngateway.common.base.repo.mysql.cmdb;

import com.glsc.ngateway.common.base.domain.mysql.cmdb.CmdbPartType;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.JpaSpecificationExecutor;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Repository
public interface CmdbPartTypeRepository extends JpaRepository<CmdbPartType, Integer>, JpaSpecificationExecutor<CmdbPartType> {
    List<CmdbPartType> findAllByPartTypeIdIn(List<Long> id);

    @Query(value = "select m from CmdbPartType m where m.isDelete = 0 " +
            "and (:#{#param.partTypeName} is null OR m.partTypeName LIKE CONCAT('%', :#{#param.partTypeName}, '%'))")
    Page<CmdbPartType> searchByParam(Pageable pageable, CmdbPartType param);

    @Transactional
    @Modifying
    @Query(value = "update t_cmdb_part_type set is_delete = :deleted where part_type_id in :codes ", nativeQuery = true)
    void updateIsDeleteByCodes(Integer deleted, List<String> codes);

    List<CmdbPartType> findByIsDelete(Integer noDeleted);
}

